package com.aidex.biz.controller;

import com.aidex.biz.domain.PushRuleUser;
import com.aidex.biz.service.PushRuleUserService;
import com.aidex.common.annotation.Log;
import com.aidex.common.constant.Constants;
import com.aidex.common.core.controller.BaseController;
import com.aidex.common.core.domain.R;
import com.aidex.common.core.page.PageDomain;
import com.aidex.common.enums.BusinessType;
import com.aidex.common.utils.SecurityUtils;
import com.aidex.common.utils.poi.ExcelUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 规则用户对应Controller
 *
 * @author sean
 * @email 2074844820@qq.com
 * @date 2025-03-11
 */
@RestController
@RequestMapping("/biz/bizPushRuleUser")
public class PushRuleUserController extends BaseController {
    @Autowired
    private PushRuleUserService pushRuleUserService;

    /**
     * 查询规则用户对应列表
     */
    @GetMapping("/list")
    public R<PageInfo> list(PushRuleUser pushRuleUser, HttpServletRequest request, HttpServletResponse response) {
        if (SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            pushRuleUser.setCompanyId(null);
        } else {
            pushRuleUser.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        }
        pushRuleUser.setPage(new PageDomain(request, response));
        return R.data(pushRuleUserService.findPage(pushRuleUser));
    }

    /**
     * 获取规则用户对应详细信息
     */
    @GetMapping(value = "/{id}")
    public R<PushRuleUser> detail(@PathVariable("id") String id) {
        return R.data(pushRuleUserService.get(id));
    }

    /**
     * 新增规则用户对应
     */
    @Log(title = "规则用户对应", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody @Validated PushRuleUser pushRuleUser) {
        return R.status(pushRuleUserService.save(pushRuleUser));
    }

    /**
     * 修改规则用户对应
     */
    @Log(title = "规则用户对应", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody @Validated PushRuleUser pushRuleUser) {
        return R.status(pushRuleUserService.save(pushRuleUser));
    }


    /**
     * 删除规则用户对应
     */
    @Log(title = "规则用户对应", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable String[] ids) {
        return R.status(pushRuleUserService.deletePushRuleUserByIds(ids));
    }

    /**
     * 获取最大编号
     */
    @GetMapping("/findMaxSort")
    public R findMaxSort() {
        return R.data(pushRuleUserService.findMaxSort(new PushRuleUser()));
    }

    /**
     * 导出规则用户对应列表
     */
    @Log(title = "规则用户对应", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public R export(PushRuleUser pushRuleUser) {

        if (SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            pushRuleUser.setCompanyId(null);
        } else {
            pushRuleUser.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        }
        List<PushRuleUser> list = pushRuleUserService.findList(pushRuleUser);
        ExcelUtil<PushRuleUser> util = new ExcelUtil<PushRuleUser>(PushRuleUser.class);
        return util.exportExcel(list, "规则用户对应数据");
    }


    /**
     * 根据用户id获取分配用户（用于初始化）
     */
    @GetMapping(value = "/getListByUserIds")
    public R<List<PushRuleUser>> getListByUserIds(String ids) {
        return R.data(pushRuleUserService.getListByUserIds(ids.split(";")));
    }

    /**
     * 查询用户外链规则用户列表
     */
    @GetMapping("/findUserJoinRuleList")
    public R<List<PushRuleUser>> findUserJoinRuleList(PushRuleUser pushRuleUser) {
        if (SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            pushRuleUser.setCompanyId(null);
        } else {
            pushRuleUser.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        }

        return R.data(pushRuleUserService.findUserJoinRuleList(pushRuleUser));
    }

    /**
     * 修改接单状态
     */
    @PostMapping("/changeWorkingPushRuleUser")
    public R changeWorkingPushRuleUser(@RequestBody PushRuleUser pushRuleUser) {
        pushRuleUser.setUserId(SecurityUtils.getUserId());
        pushRuleUser.setDeptId(SecurityUtils.getDeptId());
        if (pushRuleUserService.changeWorkingPushRuleUser(pushRuleUser) > 0) {
            return R.success(Constants.DEFAULT_SUCCESS_MESSAGE);
        } else {
            return R.fail("请先到业务设置-分配设置添加该用户");
        }
    }
}
